package cn.itsource.user.service.impl;

import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.MD5Utils;
import cn.itsource.basic.util.StrUtils;
import cn.itsource.user.constant.UserConstants;
import cn.itsource.user.domain.LoginInfo;
import cn.itsource.user.domain.User;
import cn.itsource.user.dto.UserDto;
import cn.itsource.user.mapper.LoginInfoMapper;
import cn.itsource.user.mapper.UserMapper;
import cn.itsource.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService{

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LoginInfoMapper loginInfoMapper;

    @Autowired
    @Qualifier("redisTemplate")
    private RedisTemplate redisTemplate;

    //@TODO 自己做，我不做，你们做
    @Override
    public void add(User user) {
        super.add(user);
    }

    @Override
    public void update(User user) {
        super.update(user);
    }

    @Override
    public void del(Long id) {
        super.del(id);
    }

    @Transactional
    @Override
    public void phoneRegister(UserDto userDto) {

        //1 基本校验
        //1.1 空检验
        if (StringUtils.isEmpty(userDto.getPhone())||StringUtils.isEmpty(userDto.getVerifyCode())
                ||StringUtils.isEmpty(userDto.getPassword())||StringUtils.isEmpty(userDto.getConfirmPwd()))
            throw new BusinessException("请输入相关信息后再注册！");
        //1.2 两个密码要一致
        if (!userDto.getPassword().equals(userDto.getConfirmPwd()))
            throw new BusinessException("两次密码不一致！");
        //1.3 判断是否已经被注册
        User userTmp = userMapper.loadByPhone(userDto.getPhone());
        if (userTmp!=null)
            throw new BusinessException("用户已经被注册！");
        //2 验证验证码-吧输入验证码和redis中存的验证码做比对（不要区分大小写）
        Object codeObj = redisTemplate.opsForValue().get(UserConstants.REGISTER_CODE_PREFIX + userDto.getPhone());
        if (codeObj==null)
            throw new BusinessException("验证码已失效！请重新发送！");
        String code = ((String)codeObj).split(":")[0];
        if (!userDto.getVerifyCode().equalsIgnoreCase(code))
            throw new BusinessException("验证码不正确！请重新输入！");
        //3 注册
        User user = loginDto2User(userDto);
        LoginInfo info = user2LoginInfo(user);
        //3.2 登录信息表
        loginInfoMapper.save(info);
        user.setInfo(info);
        //3.1 用户表
        userMapper.save(user);

    }

    //1 申明要返回对象
    //2 进行转换
    //3 返回
    private LoginInfo user2LoginInfo(User user) { //先地哦啊用的是loginDto2User，里面已经处理salt+password
        LoginInfo info = new LoginInfo();
        BeanUtils.copyProperties(user,info); //按照同名原则拷贝属性
        return info;
    }

    //1 申明要返回对象
    //2 进行转换
    //3 返回
    private User loginDto2User(UserDto userDto) {
        //1 申明要返回对象
        User user = new User();
        //2 进行转换
        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());
        String salt = StrUtils.getComplexRandomString(32);
        String securityPwd = MD5Utils.encrypByMd5(userDto.getPassword()+salt);
        user.setSalt(salt);
        user.setPassword(securityPwd);
        //3 返回
        return user;
    }
}
